<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>请假审批</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="../../layuimini/lib/layui-v2.5.5/css/layui.css" media="all" />
  <link rel="stylesheet" href="../../layuimini/css/public.css" media="all" />
</head>

<body>
  <div class="layuimini-container">
    <div class="layuimini-main">
      <fieldset class="table-search-fieldset">
        <legend>搜索信息</legend>
        <div style="margin: 10px 10px 10px 10px">
          <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">职工:</label>
                <div class="layui-input-inline">
                  <select name="personalId" id="selectId" lay-search="">
                    <option value="">请选择请假人</option>
                  </select>
                </div>
              </div>

              <div class="layui-inline">
                <label class="layui-form-label">请假类型:</label>
                <div class="layui-input-block">
                  <select name="leaveType" id="type">
                    <option value="">请选择请假类型</option>
                    <option value="1">事假</option>
                    <option value="2">年假</option>
                    <option value="3">婚假</option>
                    <option value="4">产假</option>
                    <option value="5">陪产假</option>
                    <option value="6">丧假</option>
                    <option value="7">调休假</option>
                    <option value="8">其它</option>
                  </select>
                </div>
              </div>

              <div class="layui-inline">
                <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn">
                  <i class="layui-icon layui-icon-search"></i> 搜 索
                </button>

                <button type="button" class="layui-btn layui-btn-primary" id="data-reset-btn">
                  <i class="layui-icon"></i> 重 置
                </button>
              </div>
            </div>
          </form>
        </div>
      </fieldset>

      <script type="text/html" id="toolbarDemo">
          <div class="layui-btn-group">

            <button
              class="layui-btn layui-btn-sm layui-btn-warm"
              lay-event="refresh"
            >
              刷新
            </button>
            <button
              class="layui-btn layui-btn-sm layui-btn-normal"
              lay-event="export"
            >
              导出全部数据
            </button>
          </div>
        </script>

      <div>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">
        </table>
      </div>

      <script type="text/html" id="currentTableBar">
          <div
            class="layui-btn layui-btn-normal layui-btn-xs"
            lay-event="approve">
            审批
          </div>
          
        </script>
    </div>
  </div>

  <script type="text/html" id="approveDialog">
      <form
        id="approveForm"
        lay-filter="approveForm"
        class="layui-form layui-form-panes"
        style="padding-right: 30px;">

        <div class="layui-form layuimini-form">

          <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <input name="leaveId" type="hidden" />
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label required">审批结果:</label>
            <div class="layui-input-block">
              <input
              title="通过"
                type="radio"
                name="leaveStatus"
                id="status"
                class="layui-input"
                lay-verify="required"
                checked
                value="2"
              />
              <input
              title="驳回"
                type="radio"
                name="leaveStatus"
                id="status"
                class="layui-input"
                lay-verify="required"
                value="3"
              />
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
              <textarea
                name="approveRemark"
                placeholder="备注"
                class="layui-textarea"
              ></textarea>
            </div>
          </div>

          <div class="layui-form-item ">
            <label class="layui-form-label"></label>
            <button
              class="layui-btn layui-btn-normal"
              lay-filter="approve"
              lay-submit
            >
              提交
            </button>
            <button
              class="layui-btn layui-btn-primary"
              type="button"
              ew-event="closeDialog"
              id="Cancel"
            >
              取消
            </button>
          </div>


        </div>

           

      </form>
    </script>

  <script src="../../layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
  <script src="../../layuimini/js/lay-config.js" charset="utf-8"></script>

  <script>
    layui.use(
      ["layer", "form", "table", "laydate", "upload", "common", "excel"],
      function () {
        var $ = layui.jquery,
          form = layui.form,
          laydate = layui.laydate,
          table = layui.table,
          upload = layui.upload,
          common = layui.common,
          excel = layui.excel;

        tableIns = table.render({
          elem: "#currentTableId",
          url: common.api + "/leave/paged-list",
          where: { token: common.getToken, leaveStatus: 1 },
          toolbar: "#toolbarDemo",
          title: "待审批请假单信息",
          limits: [1, 5, 10, 15, 20],
          limit: 10,
          page: true,
          skin: "row",
          even: true,
          cols: [
            [

              {
                field: "leaveId",
                title: "请假ID",
                width: 100,
                sort: true,
                align: "center",
              },
              {
                field: "leaverName",
                // width: 100,
                title: "职工姓名",
                align: "center",
              },
              {
                field: "leaveType",
                // width: 100,
                title: "请假类型",
                align: "center",
                templet: function (d) {
                  if (d.leaveType == 1) {
                    return "事假";
                  } else if (d.leaveType == 2) {
                    return "年假";
                  } else if (d.leaveType == 3) {
                    return "婚假";
                  } else if (d.leaveType == 4) {
                    return "产假";
                  } else if (d.leaveType == 5) {
                    return "陪产假";
                  } else if (d.leaveType == 6) {
                    return "丧假";
                  } else if (d.leaveType == 7) {
                    return "调休假";
                  } else if (d.leaveType == 8) {
                    return "其它";
                  }
                  return "";
                },
              },
              {
                field: "leaveStartTime",
                // width: 120,
                title: "请假开始日期",
                align: "center",
                sort: true,
              },
              {
                field: "leaveEndTime",
                // width: 120,
                title: "请假结束日期",
                align: "center",
                sort: true,
              },
              {
                field: "reason",
                // width: 150,
                title: "请假原因",
                align: "center",
              },
              {
                field: "leaveStatus",
                // width: 100,
                title: "状态",
                align: "center",
                sort: true,
                templet: function (d) {
                  if (d.leaveStatus == 1) {
                    return '<span class="layui-badge layui-bg-black">待审批</span>'
                  } else if (d.leaveStatus == 2) {
                    return '<span class="layui-badge layui-bg-green">审批通过</span>'
                  } else if (d.leaveStatus == 3) {
                    return '<span class="layui-badge layui-bg-red">审批拒绝</span>'
                  }
                  return "";
                },
              },

              {
                title: "操作",
                width: 80,
                toolbar: "#currentTableBar",
                align: "center",
              },
            ],
          ],
          parseData: function (res) {
            if (res.code != 200) {
              common.login();
            }
            return {
              code: res.code, //解析接口状态
              msg: res.msg, //解析提示文本
              count: res.data.total, //解析数据长度
              data: res.data.list, //解析数据列表
            };
          },
          response: {
            statusCode: 200, //重新规定成功的状态码为 200，table 组件默认为 0
          },
          request: {
            pageName: "page", //页码的参数名称，默认：page
            limitName: "size", //每页数据量的参数名，默认：limit
          },
          initSort: {
            field: "leaveId",
            type: "asc",
          },
        });

        //监听工具栏添加删除刷新
        table.on("toolbar(currentTableFilter)", function (obj) {
          if (obj.event === "refresh") {
            tableIns.reload({
              url: common.api + "/leave/paged-list",
              // where: {},
            });
          } else if (obj.event === "export") {
            common.ajax(common.api + "/leave/list", { leaveStatus: 2 }, function (res) {
              var data = res.data;
              $.each(data, function (index, item) {
                if (item.leaveType == 1) {
                  item.leaveType = "事假";
                } else if (item.leaveType == 2) {
                  item.leaveType = "年假";
                } else if (item.leaveType == 3) {
                  item.leaveType = "婚假";
                } else if (item.leaveType == 4) {
                  item.leaveType = "产假";
                } else if (item.type == 5) {
                  item.type = "陪产假";
                } else if (item.leaveType == 6) {
                  item.leaveType = "丧假";
                } else if (item.leaveType == 7) {
                  item.leaveType = "调休假";
                } else if (item.leaveType == 8) {
                  item.leaveType = "其它";
                }

                if (item.leaveStatus == 1) {
                  item.leaveStatus = "已提交";
                } else if (item.leaveStatus == 2) {
                  item.leaveStatus = "审批通过";
                } else if (item.leaveStatus == 3) {
                  item.leaveStatus = "审批拒绝";
                }
              });
              data = common.compare(data);
              data = excel.filterExportData(data, [
                "leaveId",
                "leaverName",
                "leaveType",
                "leaveStartTime",
                "leaveEndTime",
                "reason",
                "leaveStatus",
              ]);
              data.unshift({
                leaveId: "编号",
                leaverName: "职工姓名",
                leaveType: "请假类型",
                leaveStartTime: "请假开始日期",
                leaveEndTime: "请假结束日期",
                reason: "请假原因",
                leaveStatus: "状态",

              });
              var timestart = Date.now();
              excel.exportExcel(
                {
                  sheet1: data,
                },
                "待审核请假单列表.xlsx",
                "xlsx"
              );
              var timeend = Date.now();

              var spent = (timeend - timestart) / 1000;
              layer.msg("导出成功", { icon: 1 });
            });
          }
        });

        //监听编辑删除
        table.on("tool(currentTableFilter)", function (obj) {
          if (obj.event === "approve") {
            showApproveDialog(obj.data);
            // layer.alert(JSON.stringify(obj.data))
          }
        });

        function showApproveDialog(formData) {
          var idx = layer.open({
            type: 1,
            area: ["500px", "320px"],
            title: "审批",

            anim: 1,
            shade: 0.6,
            content: $("#approveDialog").html(),
            success: function (layero, dIndex) {
              // 回显表单数据
              form.val("approveForm", { leaveId: formData.leaveId });
              // 表单提交事件
              form.on("submit(approve)", function (data) {
                // 字符串转换成数字
                common.ajax(
                  common.api + "/leave/approve",
                  JSON.stringify(data.field),
                  function (res) {
                    if (res.code === 200) {
                      layer.close(dIndex);
                      layer.msg(res.msg, { icon: 1 });
                      setTimeout(() => {
                        table.reload("currentTableId");
                      }, 1000);
                    } else {
                      layer.msg(res.msg, { icon: 2 });
                    }
                  },
                  "put",
                  function (data) {
                    layer.msg(data.msg, { icon: 2 });
                  },
                  false,
                  true
                );
                return false;
              });

              $("#Cancel").click(function () {
                layer.close(idx);
              });
            },
          });
        }

        // 监听搜索操作
        form.on("submit(data-search-btn)", function (data) {
          var param = data.field;
          // 状态默认待审批
          param.leaveStatus = 1;
          // return false
          // 执行搜索重载
          tableIns.reload({
            url: common.api + "/leave/paged-list",
            method: "get",
            where: param,
          });
          return false;
        });

        var selectData;
        layer.ready(function () {
          // select 
          common.ajax(common.api + "/personal/all", null, function (data) {
            selectData = data.data;
            $.each(selectData, function (index, item) {
              var option = new Option(item.name, item.personalId);
              $("#selectId").append(option);
            });
            form.render("select");
          });
          // 监听重置操作
          $("#data-reset-btn").click(function () {
            $("#selectId").val(0);
            $("#type").val(0);
            form.render("select");
            tableIns.reload({
              url: common.api + "/leave/paged-list",
              method: "get",
              where: { personalId: "", leaveType: "" },
            });
          });
        });
      }
    );
  </script>
</body>

</html>